package com.babel.util.tools;

import java.text.SimpleDateFormat;
import java.util.Date;


public class WLDCodeTools {

	/**
	 * 锁对象，可以为任意对象
	 */
	private static Object lockObj = "lockerOrder";
	/**
	 * 订单号生成计数器
	 */
	private static long orderNumCount = 0L;
	/**
	 * 每秒生成订单号数量最大值
	 */
	private static int maxPerMSECSize = 9999;

	public static String makeOrderNum() {
		try {
			// 最终生成的订单号
			String finOrderNum = "";
			synchronized (lockObj) {
				// 取系统当前时间作为订单号变量前半部分，精确到秒
				long nowLong = Long.parseLong(new SimpleDateFormat("yyMMddHHmmss").format(new Date()));
				// 计数器到最大值归零，可扩展更大，目前1秒处理峰值9999个
				if (orderNumCount >= (maxPerMSECSize)) {
					orderNumCount = 0L;
				}
				// 组装订单号
				// String countStr=maxPerMSECSize +orderNumCount+"";
				// finOrderNum="ZBD"+nowLong+countStr.substring(1);
				String countStr = String.format("%04d", orderNumCount);
				finOrderNum = "WLD" + nowLong + countStr;
				orderNumCount++;
				// System.out.println(finOrderNum + "--" +
				// Thread.currentThread().getName() + "::" + tname );
				// Thread.sleep(1000);
				// System.out.println(finOrderNum + "--");
				return finOrderNum;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	

}
